﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="FormsUtilities.cs" company="" />
// <summary>
//   Miscelleneous utilities for Windows Forms environment.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace FormsUtilities
{
    using System;
    using System.Windows.Forms;

    /// <summary>
    /// Miscelleneous utilities for Windows Forms environment.
    /// </summary>
    public static class FormsUtilitiesSet
    {
        /// <summary>
        /// Since derivatives of <see cref="ToolStripItem"/> have properties sharing functionality definition, but not type hierarchy or interface implementation,
        /// This ugly patch is forced in order to set such items' properties.
        /// </summary>
        /// <param name="item">The item to act upon.</param>
        /// <param name="menuActivity">An action for a menu-type of item.</param>
        /// <param name="buttonActivity">An action for a button-type of item.</param>
        public static void ActOnToolStripItem(ToolStripItem item, Action<ToolStripMenuItem> menuActivity, Action<ToolStripButton> buttonActivity)
        {
            var menuItem = item as ToolStripMenuItem;

            if (menuItem != null)
            {
                menuActivity(menuItem);
            }
            else
            {
                var buttonItem = item as ToolStripButton;

                if (buttonItem != null)
                {
                    buttonActivity(buttonItem);
                }
            }
        }
    }
}
